<template>
  <div class="goodsConfig h100" v-loading="loading">
    <div class="pageContent">
      <el-form
      class="form"
        :model="ruleForm"
        :inline="true"
        ref="ruleForm"
        label-width="80px"
      >
        <el-row :gutter="11" class="fileSearch">
          <!-- <el-form-item label="手机号:" prop="loginPhone">
            <el-input v-model="ruleForm.loginPhone" clearable></el-input>
          </el-form-item> -->
          <el-form-item label="类型:" prop="newsType">
            <!-- <el-input v-model="ruleForm.newsType" clearable></el-input> -->
            <el-select v-model="ruleForm.newsType" placeholder="请选择类型">
              <el-option label="全部" value=""></el-option>
              <el-option label="殡葬法规" :value="0"></el-option>
              <el-option label="殡葬文化" :value="1"></el-option>
              <el-option label="殡葬服务" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              @click="onSubmit('search')"
            >
              查询
            </el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
            <el-button
              icon="el-icon-plus"
              type="success"
              @click="dialogVisible = true"
              >新增</el-button
            >
          </el-form-item>
        </el-row>
        <div class="table_wrapper">
          <el-table
          :data="tableData"
          border
          :header-cell-style="{
            background: 'rgb(246,247,251)',
            color: '#606266'
          }"
          stripe
          class="defaultTableHeight"
        >
          <el-table-column type="index" label="编号" width="50">
          </el-table-column>
          <el-table-column
            prop="title"
            align="center"
            label="标题"
            width="200px"
          >
          </el-table-column>
          <el-table-column align="center" label="内容">
            <template slot-scope="scope">
              <p class="content" v-html="scope.row.content"></p>
            </template>
          </el-table-column>
          <el-table-column
            prop="newsType"
            align="center"
            label="类型"
            width="120px"
          >
            <template slot-scope="scope">
              {{
                scope.row.newsType == 0
                  ? "殡葬法规"
                  : scope.row.newsType == "1"
                  ? "殡葬文化"
                  : "殡葬服务"
              }}
            </template>
          </el-table-column>

          <!-- <el-table-column prop="asLogin" align="center" label="禁用启用">
            <template slot-scope="scope">
              <el-button
                :type="scope.row.asLogin === 0 ? 'danger' : 'success'"
                size="mini"
                >{{ scope.row.asLogin === 0 ? "禁用" : "启用" }}
              </el-button>
            </template>
          </el-table-column> -->
          <el-table-column label="操作" align="center" width="220px">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="uploadGoods(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="delGoods(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <p style="text-align: right;">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pager.currentPage"
            :page-size="pager.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            background
            layout="total,sizes,prev, pager, next, jumper"
            :total="pager.total"
          >
          </el-pagination>
        </p>
        </div>
        
      </el-form>
    </div>
    <el-dialog
      title="添加商品类型"
      :visible.sync="dialogVisible"
      width="800px"
      v-dialogDrag
      @close="closeDialog"
      @opened="opened"
    >
      <el-form
        ref="goodsForm"
        :model="goodsForm"
        :rules="goodsFormRules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="title">
          <el-input ref="inputTitle" v-model="goodsForm.title" :maxlength='30'></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="newsType">
          <el-select v-model="goodsForm.newsType" placeholder="请选择类型">
            <el-option label="殡葬法规" :value="0"></el-option>
            <el-option label="殡葬文化" :value="1"></el-option>
            <el-option label="殡葬服务" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor
            ref="editor"
            @change="changeEditor"
            :value="goodsForm.content"
          ></editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveGoods">保存</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// import * as appUser from "@/api/appUser";
import { line_list, line_trends, line_delete } from "@/api/industryNewsManage";
import editor from "@/components/Editor";
export default {
  name: "industryNewsManage",
  data() {
    return {
      loading: false,
      dialogVisible: false,
      ruleForm: {
        newsType: ""
      },
      goodsForm: {
        id: "",
        title: "",
        content: "",
        newsType: 1
      },
      pager: {
        pageSize: 20,
        total: 1,
        currentPage: 1
      },
      tableData: [],
      goodsFormRules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }]
      }
    };
  },
  components: {
    editor
  },
  mounted() {
    this.onSubmit();
  },
  //注销window.onresize事件
  destroyed() {},
  methods: {
    async onSubmit() {
      this.loading = true;
      let postData = {
        newsType: this.ruleForm.newsType,
        pageSize: this.pager.pageSize,
        total: this.pager.total,
        currentPage: this.pager.currentPage
      };
      try {
        const res = await line_list(postData);
        console.log(res, 200);
        this.loading = false;
        if (res.code !== "2000") {
          this.loading = false;
          this.$message(res.msg ? res.msg : "数据读取失败");
          return;
        }

        this.tableData = res.data;
        this.pager.pageSize = res.pageSize;
        this.pager.total = res.total;
        this.pager.currentPage = res.currentPage;
        //数据读取成功
      } catch (err) {
        this.$message.error(err.msg);
        console.log(err, 404);
        this.loading = false;
        this.tableData = [];
      }
    },

    uploadGoods(info) {
      this.dialogVisible = true;
      this.goodsForm = JSON.parse(JSON.stringify(info));
    },
    changeEditor(html) {
      this.goodsForm.content = html;
    },
    // 保存
    async saveGoods() {
      this.$refs["goodsForm"].validate(async valid => {
        if (valid) {
          this.loading = true;
          let postData = this.goodsForm;
          console.log(postData, "postData");
          try {
            const res = await line_trends(postData);
            console.log(res, 200);
            this.loading = false;
            if (res.code !== "2000") {
              this.$message.error(res.msg ? res.msg : "操作失败");
              return;
            }
            this.$message.success("操作成功");
            this.onSubmit();
            this.closeDialog();
          } catch (err) {
            this.loading = false;
            console.log(err, 404);
            this.$message.error(err.msg);
          }
        }
      });
    },
     delGoods(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
           let postData = { ids: [id] };
      console.log(postData, "postData");
      try {
        const res = await line_delete(postData);
        console.log(res, 200);
        if (res.code !== "2000") {
          this.$message.error(res.msg ? res.msg : "删除失败");
          return;
        }
        this.$message.success("删除成功");
        this.onSubmit();
      } catch (err) {
        this.$message.error(err.msg);
        console.log(err, 404);
      }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
    },
    // 获取焦点
    opened() {
      this.$refs.inputTitle.focus();
    },
    // 关闭弹窗的回调
    closeDialog() {
      this.goodsForm = { id: "", title: "", content: "", newsType: 1 };
      this.$refs["goodsForm"].resetFields();
      this.dialogVisible = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["ruleForm"].resetFields();
      this.onSubmit();
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      this.pager.pageSize = size;
      this.onSubmit();
    },
    handleCurrentChange: function(currentPage) {
      this.pager.currentPage = currentPage;
      this.onSubmit(); //点击第几页
    }
  }
};
</script>

<style lang="scss" scoped>
.pageContent{
  .form{
    height: 100%;
  }
}
.el-button {
  margin-left: 20px;
}
p.content {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  max-height: 200px;
  overflow: hidden;
}
</style>
